package com.gameloft.futbol.element.screen;

import javax.microedition.lcdui.Graphics;
import java.util.Vector;
import com.gameloft.futbol.fCanvas;
import com.gameloft.input.Keypad;
import com.gameloft.futbol.Element;
import com.gameloft.futbol.element.Screen;
import com.gameloft.futbol.element.Button;
import com.gameloft.futbol.element.Viewport;
import com.gameloft.futbol.element.viewport.TitlesViewport;
import com.gameloft.futbol.element.viewport.TournamentsViewport;
import com.gameloft.futbol.element.viewport.OthersViewport;
import com.gameloft.futbol.element.viewport.HelpViewport;
import com.gameloft.futbol.element.viewport.RecommendViewport;
import com.gameloft.futbol.element.viewport.LegalViewport;
import com.gameloft.futbol.element.viewport.ExitViewport;


public class MainScreen extends Screen
{
	
	//Main screen childs
	public final static int TITLES = 0;
	public final static int TOURNAMENT = 1;
	public final static int OTHERS = 2;
	public final static int HELP = 3;
	public final static int RECOMMEND = 4;
	public final static int LEGAL = 5;
	public final static int EXIT = 6;
	public final static int VIEWPORT = 7;
	
	
	public MainScreen()
	{
		Button titlesButton = new Button(TITLES, "Titles", "/news.png");
		Button tournamentsButton = new Button(TOURNAMENT, "Tournament", "/news.png");
		Button othersButton = new Button(OTHERS, "Others", "/ball.png");
		Button helpButton = new Button(HELP, "Help", "/ball.png");
		Button recommendButton = new Button(RECOMMEND, "Recommend", "/ball.png");
		Button legalButton = new Button(LEGAL, "Legal", "/ball.png");
		Button exitButton = new Button(EXIT, "Exit", "/ball.png");
		
		Viewport titlesViewport = new TitlesViewport();
		Viewport tournamentsViewport = new TournamentsViewport();
		Viewport othersViewport = new OthersViewport();
		Viewport helpViewport = new HelpViewport();
		Viewport recommendViewport = new RecommendViewport();
		Viewport legalViewport = new LegalViewport();
		Viewport exitViewport = new ExitViewport();
		
		appendChild(titlesButton);
		appendChild(tournamentsButton);
		appendChild(othersButton);
		appendChild(helpButton);
		appendChild(recommendButton);
		appendChild(legalButton);
		appendChild(exitButton);
		
		appendChild(titlesViewport);
		appendChild(tournamentsViewport);
		appendChild(othersViewport);
		appendChild(helpViewport);
		appendChild(recommendViewport);
		appendChild(legalViewport);
		appendChild(exitViewport);
		
		setFullScreenRect();
		
		titlesButton.setSelected(true);
		titlesButton.setHighlighted(true);
		
		titlesViewport.setVisible(true);
		tournamentsViewport.setVisible(false);
		othersViewport.setVisible(false);
		helpViewport.setVisible(false);
		recommendViewport.setVisible(false);
		legalViewport.setVisible(false);
		exitViewport.setVisible(false);
	}
	
	public void layout()
	{
		//Portrait layout
		if (w < h)
		{
			int buttonH = h/10;
			if (buttonH > 50) buttonH = 50;
			
			int buttonW = w/4;
			for (int i = TITLES; i <= HELP; i++)
			{
				int k = i - TITLES;
				Element elem = (Element)(childs.elementAt(i));
				elem.setRect(k * buttonW, 0, buttonW, buttonH);
			}
			
			buttonW = w/3;
			for (int i = RECOMMEND; i <= EXIT; i++)
			{
				int k = i - RECOMMEND;
				Element elem = (Element)(childs.elementAt(i));
				elem.setRect(k * buttonW, h - buttonH, buttonW, buttonH);
			}
			
			getCurrentViewport().setRect(0, buttonH, w, h - 2*buttonH);
		}
		//Landscape layout
		else
		{
			int buttonH = h/8;
			if (buttonH > 50) buttonH = 50;
			
			int buttonW = w/7;
			for (int i = TITLES; i <= EXIT; i++)
			{
				int k = i - TITLES;
				Element elem = (Element)(childs.elementAt(i));
				elem.setRect(k * buttonW, h - buttonH, buttonW, buttonH);
			}
			
			Element elem = (Element)(childs.elementAt(VIEWPORT));
			elem.setRect(0, 0, w, h - buttonH);
		}
	}
	
	public void update(long elapsed)
	{
		if ((fCanvas.getScreenWidth() != w) || (fCanvas.getScreenHeight() != h))
			setRect(0, 0, fCanvas.getScreenWidth(), fCanvas.getScreenHeight());
		
		if (!fCanvas.isTouchScreen())
		{
			Button b = getHighlightedButton();
			if (b != null)
			{
				if (Keypad.wasPressed(Keypad.FLAG_KEYS_LEFT))
				{
					b.setHighlighted(false);
					int id = b.getId() - 1;
					int prevId = (id < TITLES) ? EXIT : id;
					((Button)getChild(prevId)).setHighlighted(true);
				} 
				else if (Keypad.wasPressed(Keypad.FLAG_KEYS_RIGHT))
				{
					b.setHighlighted(false);
					int id = b.getId() + 1;
					int nextId = (id > EXIT) ? TITLES : id;
					((Button)getChild(nextId)).setHighlighted(true);
				}
				else if (Keypad.wasReleased(Keypad.FLAG_KEYS_SELECT))
				{
					setCurrentMenu(b.getId());
					b.setHighlighted(false);
				}
			}
			else if (Keypad.wasReleased(Keypad.FLAG_KEYS_BACK))
			{
				b = (Button)getSelectedButton();
				b.setSelected(true);
				b.setHighlighted(true);
			}
		}
	}
	
	public void setCurrentMenu(int index)
	{
		int currIndex = ((Button)getSelectedButton()).getId();
		if (index != currIndex)
		{
			((Button)getChild(currIndex)).setSelected(false);
			((Button)getChild(index)).setSelected(true);
			((Viewport)getChild(VIEWPORT+currIndex)).setVisible(false);
			((Viewport)getChild(VIEWPORT+index)).setVisible(true);
			layout();
		}
	}
	
	public Viewport getCurrentViewport()
	{
		for (int i = VIEWPORT+TITLES; i <= VIEWPORT+EXIT; i++)
		{
			Viewport v = (Viewport)childs.elementAt(i);
			if (v.isVisible()) return v;
		}
		return null;
	}
	
	public Button getSelectedButton()
	{
		for (int i = TITLES; i <= EXIT; i++)
		{
			int k = i - TITLES;
			Button b = (Button)(childs.elementAt(i));
			if (b.isSelected()) return b;
		}
		return null;
	}
	
	public Button getHighlightedButton()
	{
		for (int i = TITLES; i <= EXIT; i++)
		{
			int k = i - TITLES;
			Button b = (Button)(childs.elementAt(i));
			if (b.isHighlighted()) return b;
		}
		return null;
	}
	
}